﻿Imports System.Configuration
Public Class ConfigureZebraLabelPrinting

    Private Sub ConfigureZebraLabelPrinting_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        LoadConfigSettings()
        m_dirty = False
    End Sub

    Sub LoadConfigSettings()
        With My.Settings
            tbEmailServer.Text = .ImapServer
            tbEmailPort.Text = .ImapServerPort
            tbUserName.Text = .EmailUserName
            tbPassword.Text = .EmailPassword

            tbLabelLength.Text = .LabelLength
            tbLabelWidth.Text = .LabelWidth

            comboboxDensity.SelectedItem = .Density

            tbEmailSeparator.Text = .separatorString

            tbXcoord.Text = .xcoord

            tbCharacterWrap.Text = .textWrapCtr

            tbYCoord.text = .ycoord
            tbYCoordAppend.text  = .yCoordAppend
            comboboxtopLabelFont.selecteditem = .TopPartFont

            tbYCoordLowerTag.Text = .yCoordLowerTag
            tbYCoordLowerAppend.Text = .yCoordLowerAppend
            comboboxLowerLabelFont.SelectedItem = .LowPartFont

        End With
        
    End Sub

    Private Sub btnSaveSettings_Click(sender As System.Object, e As System.EventArgs) Handles btnSaveSettings.Click
        With My.Settings
            .ImapServer = tbEmailServer.Text
            .ImapServerPort = tbEmailPort.Text
            .EmailUserName = tbUserName.Text
            .EmailPassword = tbPassword.Text

            .LabelLength = tbLabelLength.Text
            .LabelWidth = tbLabelWidth.Text

            .Density = comboboxDensity.SelectedItem

            .separatorString = tbEmailSeparator.Text

            .xcoord = tbXcoord.Text
            .textWrapCtr = tbCharacterWrap.Text

            .ycoord = tbYCoord.Text
            .yCoordAppend = tbYCoordAppend.Text
            .TopPartFont = comboboxtopLabelFont.SelectedItem

            .yCoordLowerTag = tbYCoordLowerTag.Text
            .yCoordLowerAppend = tbYCoordLowerAppend.Text
            .LowPartFont = comboboxLowerLabelFont.SelectedItem

        End With
        My.Settings.Save()
        m_dirty = False
    End Sub

    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        If m_dirty Then
            If MsgBox("Data has been changed, would you like to save before closing?", MsgBoxStyle.YesNo, "Data change detected") = MsgBoxResult.Ok Then
                My.Settings.Save()
            End If
        End If
        Me.Close()
    End Sub

    Private m_dirty As Boolean

    Private Sub Form_Changed(ByVal sender As Object, _
                             ByVal e As EventArgs) Handles tbEmailServer.TextChanged,
                         tbEmailPort.TextChanged,
                         tbUserName.TextChanged,
                         tbPassword.TextChanged,
                         tbLabelLength.TextChanged,
                         tbLabelWidth.TextChanged,
                         comboboxDensity.SelectedIndexChanged,
                         tbEmailSeparator.TextChanged,
                         tbCharacterWrap.TextChanged,
                         tbXcoord.TextChanged,
                         tbYCoord.TextChanged,
                         tbYCoordAppend.TextChanged,
                         comboboxtopLabelFont.SelectedIndexChanged,
                         tbYCoordLowerTag.TextChanged,
                         tbYCoordLowerAppend.TextChanged,
                         comboboxLowerLabelFont.SelectedIndexChanged
        m_dirty = True
    End Sub

    
    Private Sub btnPrintTestPage_Click(sender As System.Object, e As System.EventArgs) Handles btnPrintTestPage.Click
        Try
            Dim client As ImapX.ImapClient = New ImapX.ImapClient(tbEmailServer.Text, tbEmailPort.Text, True)
            Dim result As Boolean = False
            result = client.Connection()

            If result Then
                Console.WriteLine("@Connected")
            End If

            result = client.LogIn(tbUserName.Text, tbPassword.Text)

            Dim folders As ImapX.FolderCollection = client.Folders
            Dim mc = client.Folders("INBOX").Messages.Where(Function(x) x.Flags.Count = 0)


            For Each m As ImapX.Message In client.Folders("INBOX").Search("UNSEEN", True)
                m.Process()

                Dim textBody() As String = m.TextBody.TextData.Split(ControlChars.Lf) 'StripTags(m.HtmlBody.TextData).Split(ControlChars.Lf)

                'Print the label
                CommonClass.printLabel(textBody)
                Exit For
            Next
            MsgBox("Test page successfully sent to printer.", MsgBoxStyle.Information, "Zebra Label Printer")
        Catch ex As Exception
            MsgBox("Error encountered while trying to extract email or printing the label" & ControlChars.Lf & ControlChars.Lf & ex.Message.ToString, MsgBoxStyle.Critical, "Zebra Label Printer Failed")
        End Try
    End Sub
End Class